package com.gaohui.nestedrecyclerview.java.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gaohui.nestedrecyclerview.R;
import com.gaohui.nestedrecyclerview.java.adapter.MultiTypeAdapter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * @author : xfhy
 * Create time : 2021/3/8 10:59
 * Description :
 */
public class InforListView extends FrameLayout {
    boolean hasLoadData = false;
    private ArrayList<Object> mDataList = new ArrayList<>();
    private ChildRecyclerView rvInforList;
    private MultiTypeAdapter adapter;
    private RefreshLayout refreshLayout;

    public InforListView(@NonNull Context context) {
        this(context, null);
    }

    public InforListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InforListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_infor_list, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(view, layoutParams);


        rvInforList = findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rvInforList.setLayoutManager(linearLayoutManager);
        adapter = new MultiTypeAdapter(mDataList);
        rvInforList.setAdapter(adapter);

        refreshLayout = findViewById(R.id.swipeRefreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(context));
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                refresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadMoreData();
            }
        });
    }

    private void refresh() {
        mDataList.clear();
        initData();
        refreshLayout.finishRefresh();
    }

    private void loadMoreData() {
        for (int i = 0; i <= 20; i++) {
            mDataList.add("default child item " + i);
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
        refreshLayout.finishLoadMore();
    }

    public ChildRecyclerView getChildRecyclerView() {
        return rvInforList;
    }

    public void onUserVisibleChange(boolean isVisibleToUser) {
        if (!hasLoadData && isVisibleToUser) {
            initData();
        }
    }

    private void initData() {
        hasLoadData = true;
        for (int i = 0; i <= 30; i++) {
            mDataList.add("default child item " + i);
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

}

